﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.SharePoint.Administration;

namespace ServBus.ListForm2013.License
{
    internal class FarmInfo
    {
        private FarmInfo()
        {
        }

        public static List<SPServer> GetFrontEndServers()
        {
            List<SPServer> wfes = new List<SPServer>();
            foreach (SPServer server in SPFarm.Local.Servers)
            {
                foreach (SPServiceInstance svc in server.ServiceInstances)
                {
                    if (!svc.Hidden && !svc.SystemService &&
                        svc.Status == SPObjectStatus.Online && (svc is SPWebServiceInstance))
                    {
                        wfes.Add(server);
                        break;
                    }
                }
            }
            return wfes;
        }

        public static byte[] GetFarmInfo(List<Guid> serverIds)
        {
            byte[] result = null;
            using (MemoryStream stream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, serverIds);
                stream.Seek(0, SeekOrigin.Begin);
                result = new byte[stream.Length];
                stream.Read(result, 0, result.Length);
            }
            return result;
        }
    }
}
